package com.example.joysinfo.lkc.http;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.example.joysinfo.lkc.bean.Codebean;
import com.example.joysinfo.lkc.bean.Loginbean;
import com.example.joysinfo.lkc.bean.Regidbean;
import com.example.joysinfo.lkc.bean.UploadBean;
import com.example.joysinfo.lkc.util.AES;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;

import org.apache.http.Header;
import org.apache.http.entity.StringEntity;
import org.json.JSONObject;

import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;


/**
 * Created by Administrator on 2016/5/16.
 */
public class LikoushanApi {


    private static String getCodeurl="http://60.232.1.10:83/likoushan_c/app/getLoginCode.do";

    private static String loginbUrl ="http://60.232.1.10:83/likoushan_c/app/login.do";

    private static String uploadlocation ="http://60.232.1.10:83/likoushan_c/inter/upcoor.do";

    private static String sendreg ="http://60.232.1.10:83/likoushan_c/app/addRegId.do";

    private static String cleardreg ="http://60.232.1.10:83/likoushan_c/app/deleteRegId.do";


    public static String imei = "";

    private static AsyncHttpClient client= new AsyncHttpClient();


    /***
     * 登陆
     *
     * */
    public static void login(Context context,String code,String name,String password,final ResponseInterface res) {


        client.setTimeout(20 * 1000);
        StringEntity entity = null;
        JSONObject json = new JSONObject();

        try {
            name = AES.encrypt(name, "d8f44230260a4825");
            password= AES.encrypt(password, "d8f44230260a4825");
        } catch (Exception e) {
            e.printStackTrace();
        }

        try {
            json.put("useraccount",name);
            json.put("passwd", password);
            json.put("code", code);

        } catch (org.json.JSONException e) {
            e.printStackTrace();
        }
        try {
            entity = new StringEntity(json.toString(), "UTF-8");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        client.post(context,loginbUrl+setPara("1.0",getTim(),"n4djWcN94yE2YLmVx4soZOvnRQdsqcaeMx6KnFd6Cs4"
                ,imei,"1"), entity, "application/json", new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int i, Header[] headers, byte[] bytes) {
                if(!TextUtils.isEmpty(new String(bytes))){
                    Log.d("xing", new String(bytes));
                    Loginbean bean = JSON.parseObject(new String(bytes), Loginbean.class);
                    if (bean.getResult_code().equals("1")) {
                        res.onSuccess(bean);
                    } else {
                        res.onFailed(bean.getResult_txt());
                    }
                }
            }
            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
                res.onFailed("登录失败");
            }
        });
    }

    /***
     *   上传坐标
     *
     * */
    public static void sendLocation(Context context,String sign,String userid,String east_latitude,String north_latitude,final ResponseInterface res) {

        StringEntity entity = null;
        JSONObject json = new JSONObject();

        try {
            json.put("east_latitude", east_latitude);
            json.put("north_latitude", north_latitude);

        } catch (org.json.JSONException e) {
            e.printStackTrace();
        }
        try {
            entity = new StringEntity(json.toString(), "UTF-8");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        client.post(context, uploadlocation + setPara("1.0", getTim(), sign
                , imei, userid), entity, "application/json", new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {

                if (!TextUtils.isEmpty(new String(responseBody))) {
                    Log.d("xing", "上传坐标结果" + new String(responseBody));
                    UploadBean bean = JSON.parseObject(new String(responseBody), UploadBean.class);
                    if (bean.getResult_code().equals("1")) {
                        res.onSuccess(bean);
                    } else {
                        res.onFailed(bean.getResult_txt());
                    }
                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
                res.onFailed("登录失败");
            }
        });
    }

    /***
     *   获取验证码
     *
     * */
    public static void getCode(Context context, final ResponseInterface res) {

        String fangwen=getCodeurl+setPara("1.0",getTim(),""
                ,imei,"");
        Log.d("xing","访问地址"+fangwen);
        client.get(context, fangwen, null, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                if(!TextUtils.isEmpty(new String(responseBody))){
                    Log.d("xing", "tongzhi" + new String(responseBody));
                    Codebean bean = JSON.parseObject(new String(responseBody), Codebean.class);
                    if (bean.getResult_code().equals("1")) {
                        res.onSuccess(bean);
                    } else {
                        res.onFailed(bean.getResult_txt());
                    }
                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
                res.onFailed("获取验证码失败");
            }
        });
    }

    /***
     *   上传regid
     *
     * */
    public static void sendregid(Context context,String regid,String sign,String userid, final ResponseInterface res) {

        client.setTimeout(20 * 1000);
        String fangwen=sendreg+setPara("1.0",getTim(),sign
                ,imei,userid)+"&regId="+regid;
        Log.d("xing","访问地址"+fangwen);
        client.get(context, fangwen, null, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                if(!TextUtils.isEmpty(new String(responseBody))){
                    Log.d("xing", "tongzhi" + new String(responseBody));
                    Regidbean bean = JSON.parseObject(new String(responseBody), Regidbean.class);
                    if (bean.getResult_code().equals("1")) {
                        res.onSuccess(bean);
                    } else {
                        res.onFailed(bean.getResult_txt());
                    }
                }
            }
            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
                res.onFailed("获取验证码失败");
            }
        });
    }

    /***
     *   注销regid
     *
     * */
    public static void clearregid(Context context,String regid,String sign,String userid, final ResponseInterface res) {

        client.setTimeout(20 * 1000);
        String fangwen=cleardreg+setPara("1.0",getTim(),sign
                ,imei,userid)+"&regId="+regid;
        Log.d("xing","访问地址"+fangwen);
        client.get(context, fangwen, null, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                if(!TextUtils.isEmpty(new String(responseBody))){
                    Log.d("xing", "tongzhi" + new String(responseBody));
                    Regidbean bean = JSON.parseObject(new String(responseBody), Regidbean.class);
                    if (bean.getResult_code().equals("1")) {
                        res.onSuccess(bean);
                    } else {
                        res.onFailed(bean.getResult_txt());
                    }
                }
            }
            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
                res.onFailed("获取验证码失败");
            }
        });
    }
//    likoushandevice101
    private static String setPara(String api,String time,String sign,String imei,String userid){

        String setP="?api="+api+"&timestamp="+time+"&channel_type="+"2"+"&sign="+sign+"&imei="+imei+"&userid="+userid;
        return setP;
    }

    private static String getTim(){
        SimpleDateFormat oldFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        String string = oldFormat.format(new Date());
        Log.d("xing","时间"+string);
        return  string;
    }

}
